﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Packet.InPacket
{
    class PostAddFriendResultPacket : BaseInPackage
    {
        public PostAddFriendResultPacket(InPackage inpackage)  : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //好友用户名长度
            byte[] FriendNameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendNameLength, 0, 4);
            Offset += 4;
            //好友用户名
            byte[] FriendName = new byte[BitConverter.ToInt32(FriendNameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendName, 0, FriendName.Length);
            Offset += FriendName.Length;
            //分组ID
            byte[] ItemId = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemId, 0, 4);
            Offset += 6;
            //添加结果
            byte[] Result = new byte[1];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Result, 0, 1);
            Offset += 1;
            //好友分组ID
            byte[] FriendItemId = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendItemId, 0, 4);
            Offset += 4;
            //IP地址长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP地址
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //聊天端口
            byte[] TalkPort = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, TalkPort, 0, 4);

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("ITEMID", ItemId);
            this.DIC_CONTENT.Add("FRIENDUSERNAME", FriendName);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
            this.DIC_CONTENT.Add("TALKPORT", TalkPort);
            this.DIC_CONTENT.Add("RESULT", Result);
            this.DIC_CONTENT.Add("FRIENDITEMID", FriendItemId);
        }

        /// <summary>
        ///     获得好友用户名
        /// </summary>
        /// <returns>返回好友用户名</returns>
        public String GetFriendName()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["FRIENDUSERNAME"]);
        }

        /// <summary>
        ///     获得添加好友结果
        /// </summary>
        /// <returns>返回添加好友结果</returns>
        public bool GetResult()
        {
            return BitConverter.ToBoolean(this.DIC_CONTENT["RESULT"], 0);
        }

        /// <summary>
        ///     获得分组ID
        /// </summary>
        /// <returns></returns>
        public String GetItemId()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["ITEMID"], 0).ToString();
        }

        /// <summary>
        ///     获得好友分组ID
        /// </summary>
        /// <returns></returns>
        public String GetFriendItemId()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["FRIENDITEMID"], 0).ToString();
        }
    }
}
